import { Song } from 'player/init-songs'
import { _getMusicUrl } from 'network/detail'
import { mapState } from 'vuex';

/* 
    1. 获取歌曲url 
         调用获取歌曲的接口
         接口需要通过歌曲的id，来获取url
    2. 将获取到的歌曲，通过$emit发送出去，哪个组件想要调用这些歌曲就通过$on来监听
*/
export const playMusic = {
    computed: {
        ...mapState(['_playList'])
    },
    methods: {
        playMusic(index = 0) {
            console.log(this._playList);
            // let list = Object.assign({},this._playList)
            // console.log(list);
        let musicList; // 歌曲列表  歌曲列表中没有歌曲播放链接，通过id获取url
        let playList = []; // 播放列表
        musicList = this.musicList;
        musicList.map((item,i)=> {
            _getMusicUrl(item.id).then(res => {
                if (res.status === 200) {
                    let musicUrl = res.data.data[0].url
                    /* Song 构造函数参数 依次是 ： 下标、歌曲、url、id */
                    let song = new Song(i,item,musicUrl,item.id)
                    playList.push(song)
                    // this._playList.push(song)
                    // 获取全部歌曲url之后 播放
                    if (i == musicList.length -1) {
                        this.$bus.$emit('playMusic',playList,index,musicList)
                    }
                }
            })
        })
        },
    }
}